home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / forms / FORMS / bitmap.c next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  114 lines

  1. /*
  2.  * bitmap.c
  3.  *
  4.  * Forms Object class: BITMAP
  5.  *
  6.  * Written by: Mark Overmars
  7.  *
  8.  * Version 2.1 a
  9.  * Date: Sep 29, 1992
  10.  */
  11.  
  12. #include <malloc.h>
  13. #include "gl/gl.h"
  14. #include "forms.h"
  15.  
  16. typedef struct {
  17. int bits_w,bits_h;        /* width and height of bitmap */
  18. char *bits;            /* pointer to the bitmap */
  19. } SPEC;
  20.  
  21. static unsigned long thebits[FL_BITMAP_MAXSIZE];
  22.  
  23. static void draw_bitmap(FL_OBJECT *ob)
  24. /* draws the bitmap */
  25. {
  26.   SPEC *sp = ((SPEC *)(ob->spec));
  27.   int i,j,c,k = 0;
  28.   short r,g,b;
  29.   int xx,yy;        /* position of bitmap */
  30.  
  31.   /* Draw the box */
  32.   fl_drw_box(ob->boxtype,ob->x,ob->y,ob->w,ob->h,ob->col2,FL_BITMAP_BW);
  33.   fl_drw_text_beside(ob->align,ob->x,ob->y,ob->w,ob->h,
  34.                 ob->lcol,ob->lsize,ob->lstyle,ob->label);
  35.  
  36.   if (sp->bits_w == 0) return;
  37.   /* Create the bitmap */
  38.   for (j=0; j<sp->bits_h; j++)
  39.   {
  40.     for (i=0; i<sp->bits_w; i++)
  41.     {
  42.       if (sp->bits[k + i/8] & (1 << (i % 8))) c = ob->col1; else c = ob->col2;
  43.       if (fl_rgbmode)
  44.       {
  45.         fl_getmcolor(c,&r,&g,&b);
  46.         thebits[(sp->bits_h-j-1)*sp->bits_w+i] = 0x10000 * b + 0x100 * g + r;
  47.       }
  48.       else
  49.         thebits[(sp->bits_h-j-1)*sp->bits_w+i] = (short)c;
  50.     }
  51.     k += (sp->bits_w+7)/8;
  52.   }
  53.  
  54.   /* Calculate position and draw bitmap */
  55.   xx = (int) (ob->x + (ob->w - (float) sp->bits_w)/2.0);
  56.   yy = (int) (ob->y + (ob->h - (float) sp->bits_h)/2.0);
  57.   lrectwrite(xx,yy,xx-1+sp->bits_w,yy-1+sp->bits_h,thebits);
  58. }
  59.  
  60. static int handle_bitmap(FL_OBJECT *ob, int event, float mx, float my, char key)
  61. /* Handles an event, returns whether value has changed. */
  62. {
  63.   switch (event)
  64.   {
  65.     case FL_DRAW:
  66.         draw_bitmap(ob);
  67.     return 0;
  68.     case FL_FREEMEM:
  69.     free(ob->spec);
  70.     return 0;
  71.   }
  72.   return 0;
  73. }
  74.        
  75. /*------------------------------*/
  76.  
  77. FL_OBJECT *fl_create_bitmap(int type,float x,float y,float w,float h,char label[])
  78. /* Creates an object */
  79. {
  80.   FL_OBJECT *ob;
  81.   ob = fl_make_object(FL_BITMAP,type,x,y,w,h,label,handle_bitmap);
  82.   ob->boxtype = FL_BITMAP_BOXTYPE;
  83.   ob->col1 = FL_BITMAP_COL1;
  84.   ob->col2 = FL_BITMAP_COL2;
  85.   ob->lcol = FL_BITMAP_LCOL;
  86.   ob->align = FL_BITMAP_ALIGN;
  87.   ob->active = 0;
  88.   ob->spec = (int *) fl_malloc(sizeof(SPEC));
  89.   ((SPEC *)(ob->spec))->bits_w = 0;
  90.   return ob;
  91. }
  92.  
  93. FL_OBJECT *fl_add_bitmap(int type, float x, float y, float w, float h, char label[])
  94. /* Adds an object */
  95. {
  96.   FL_OBJECT *ob;
  97.   ob = fl_create_bitmap(type,x,y,w,h,label);
  98.   fl_add_object(fl_current_form,ob);
  99.   return ob;
  100. }
  101.  
  102. void fl_set_bitmap(FL_OBJECT *ob,int w, int h, char *b)
  103. /* Fills the bitmap with a bitmap. */
  104. {
  105.   SPEC *sp;
  106.   if (ob == NULL || ob->objclass != FL_BITMAP) return;
  107.   sp = ((SPEC *)(ob->spec));
  108.   sp->bits_w = w;
  109.   sp->bits_h = h;
  110.   sp->bits = b;
  111.   if (w*h > FL_BITMAP_MAXSIZE) sp->bits_h = FL_BITMAP_MAXSIZE / w;
  112.   fl_redraw_object(ob);
  113. }
  114.